package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.GrainsituationRecord;
import com.ruoyi.system.domain.IotCubeAc;
import com.ruoyi.system.mapper.GrainsituationRecordMapper;
import com.ruoyi.system.mapper.IotCubeAcMapper;
import com.ruoyi.system.service.IIotCubeAcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GrainWarehouseMapper;
import com.ruoyi.system.domain.GrainWarehouse;
import com.ruoyi.system.service.IGrainWarehouseService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-02
 */
@Service
public class GrainWarehouseServiceImpl extends ServiceImpl<GrainWarehouseMapper,GrainWarehouse> implements IGrainWarehouseService
{
    @Autowired
    private GrainWarehouseMapper grainWarehouseMapper;
    @Autowired
    private GrainsituationRecordMapper grainsituationRecordMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param warehouseId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public GrainWarehouse selectGrainWarehouseByWarehouseId(Long warehouseId)
    {
        return this.getById(warehouseId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param grainWarehouse 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<GrainWarehouse> selectGrainWarehouseList(GrainWarehouse grainWarehouse)
    {
        return grainWarehouseMapper.selectGrainWarehouseList(grainWarehouse);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param grainWarehouse 【请填写功能名称】
     * @return 结果
     */
    @Override
    public boolean insertGrainWarehouse(GrainWarehouse grainWarehouse)
    {
        return this.save(grainWarehouse);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param grainWarehouse 【请填写功能名称】
     * @return 结果
     */
    @Override
    public boolean updateGrainWarehouse(GrainWarehouse grainWarehouse)
    {
        return this.updateById(grainWarehouse);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param warehouseIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public boolean deleteGrainWarehouseByWarehouseIds(Long[] warehouseIds)
    {
        return super.removeByIds(Arrays.asList(warehouseIds));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param warehouseId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public boolean deleteGrainWarehouseByWarehouseId(Long warehouseId)
    {
        return this.removeById(warehouseId);
    }

    @Override
    public List<GrainWarehouse> getWarehouseList() {
        List<GrainWarehouse> list = this.list();
        if(CollectionUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        List<Long> ids = list.stream().map(GrainWarehouse::getWarehouseId).collect(Collectors.toList());
        List<GrainsituationRecord> grainsituationRecords = grainsituationRecordMapper.selectDataList(ids);
        Map<Long, List<GrainsituationRecord>> listMap = grainsituationRecords.stream().collect(Collectors.groupingBy(GrainsituationRecord::getWarehouseId));
        list.forEach(grainWarehouse -> {
            List<GrainsituationRecord> grainsituationRecordList = listMap.get(grainWarehouse.getWarehouseId());
            if(CollectionUtil.isNotEmpty(grainsituationRecordList)){
                List<GrainsituationRecord> collect = grainsituationRecordList.stream().sorted(Comparator.comparing(GrainsituationRecord::getDetectionTime).reversed()).collect(Collectors.toList());
                GrainsituationRecord grainsituationRecord = collect.get(0);
                grainWarehouse.setInTemValue(grainsituationRecord.getInTemValue());
                grainWarehouse.setInHumValue(grainsituationRecord.getInHumValue());
                grainWarehouse.setAvgTemValue(grainsituationRecord.getAvgTemValue());
            }
        });
        return list;
    }
}
